﻿using Coldairarrow.Business.FP;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using Coldairarrow.Util.Helper;
using EFCore.Sharding;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Business.AOP
{
    /// <summary>
    /// 工作流创建操作类
    /// </summary>
    public class OverFlowBeginAttribute : BaseAOPAttribute
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="BillType">票据类型</param>
        /// <param name="NodeName">获取表单ID的节点名称</param>
      public OverFlowBeginAttribute(string BillType,string NodeName="") 
        {
            _billtype = BillType;
            _nodeName = NodeName;
        }

        /// <summary>
        /// 数据类型
        /// </summary>
        private string _billtype { get; }

        /// <summary>
        /// 获取表单ID的节点名称
        /// </summary>
        private string _nodeName { get; }

        /// <summary>
        /// 操作之后执行
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override async Task After(IAOPContext context)
        {
            var op = context.ServiceProvider.GetService<IOperator>();
            var data = context.Arguments[0];
            string Id = "";// data.GetPropertyValue("Id").ToString();
            if (_nodeName == "")
            {
                if (!data.ContainsProperty("Id"))
                {
                    Id = data.ToString();
                }
                else
                {
                    Id = data.GetPropertyValue("Id").ToString();
                }
            }
            else 
            {
                var obj = data.GetPropertyValue(_nodeName);
                Id = obj.GetPropertyValue("Id").ToString();
            }
            op.WriteOverFlowTask(Id, _billtype);
            var flow = context.ServiceProvider.GetService<IBase_OverFlowBusiness>();
            DingHelper.sendMessage(await flow.GetDingInputAsync(Id, _billtype));
            await Task.CompletedTask;
        }
    }
}
